﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using JXT.PrimaryKey.Batman.Extensions;
using JXT.PrimaryKey.Batman.Core.Security;
using JXT.PrimaryKey.Batman.Core.Language;
using JXT.PrimaryKey.Batman.Domain.UserSystem.Models;
using JXT.PrimaryKey.Batman.Web.Mvc.Controllers;
using System.Text.RegularExpressions;

namespace JXT.PrimaryKey.Batman.Web.Mvc.Extensions
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString MvcString(this HtmlHelper html, string s)
        {
            if (html.ViewContext.Controller is BaseController)
            {
                var context = (html.ViewContext.Controller as BaseController).Context;
                return MvcHtmlString.Create(context.String(s));
            }
            return MvcHtmlString.Create(s);
        }

        public static MvcHtmlString MvcString<T>(this HtmlHelper<T> html, string s)
        {
            if (html.ViewContext.Controller is BaseController)
            {
                var context = (html.ViewContext.Controller as BaseController).Context;
                return MvcHtmlString.Create(context.String(s));
            }
            return MvcHtmlString.Create(s);
        }

        public static string String(this HtmlHelper html, string s)
        {
            if (html.ViewContext.Controller is BaseController)
            {
                var context = (html.ViewContext.Controller as BaseController).Context;
                return context.String(s);
            }
            return System.String.Empty;
        }

        public static string String<T>(this HtmlHelper<T> html, string s)
        {
            if (html.ViewContext.Controller is BaseController)
            {
                var context = (html.ViewContext.Controller as BaseController).Context;
                return context.String(s);
            }
            return System.String.Empty;
        }

        public static bool IsLogin(this HtmlHelper html)
        {
            var membership = GetMembership(html);
            if (membership == null)
                return false;
            return membership.IsLogin;
        }

        public static SecurityCookie GetCookie(this HtmlHelper html)
        {
            var membership = GetMembership(html);
            if (membership == null)
                return SecurityCookie.Default;
            return membership.Cookie;
        }

        public static SessionUser GetSession(this HtmlHelper html)
        {
            var membership = GetMembership(html);
            if (membership == null)
                return null;
            return membership.Session;
        }

        private static IMembership GetMembership(HtmlHelper html)
        {
            var controller = html.ViewContext.Controller;
            if (controller is BaseController)
            {
                return (controller as BaseController).Membership;
            }
            return null;
        }

        public static MvcHtmlString GenderString(this HtmlHelper html, Gender gender)
        {
            string key = "SystemWeb_CommonString_Gender_Unknown";
            switch (gender)
            {
                case Gender.Unknown:
                    key = "SystemWeb_CommonString_Gender_Unknown";
                    break;
                case Gender.Male:
                    key = "SystemWeb_CommonString_Gender_Male";
                    break;
                case Gender.Female:
                    key = "SystemWeb_CommonString_Gender_Female";
                    break;
            }
            return html.MvcString(key);
        }

        public static string ToFormatString(this object obj)
        {
            string s = Convert.ToString(obj);
            if (s == "0")
            {
                return "-";
            }
            return System.String.Format("{0:#0.00}", obj);
        }

        public static string ToString<T>(this Nullable<T> obj, string format = "", string defaultValue = "-") where T : struct
        {
            if (obj == null)
            {
                return defaultValue;
            }
            if (string.IsNullOrWhiteSpace(format))
            {
                return obj.Value.ToString();
            }
            else
            {
                return string.Format("{0:" + format + "}", obj);
            }
        }

        public static string ToGenderString(this Gender gender)
        {
            switch (gender)
            {
                case Gender.Female:
                    return "女";
                case Gender.Male:
                    return "男";
                default:
                case Gender.Unknown:
                    return "未知";
            }
        }

        /// <summary>
        /// 创建页码URL
        /// </summary>
        /// <param name="url">urlHelper对象</param>
        /// <param name="route">需要处理的RouteValueDictionary对象</param>
        /// <param name="page">要显示的页码</param>
        /// <param name="pageParameterName">获取页面参数的url参数名</param>
        /// <returns>给出页码的URL</returns>
        public static MvcHtmlString CreatePageUrl(this UrlHelper url, RouteValueDictionary route, int page, string pageParameterName = "page")
        {
            object defaultPageObject;
            bool haveDefaultPage = route.TryGetValue(pageParameterName, out defaultPageObject);
            route[pageParameterName] = page;
            string result = url.RouteUrl(route);
            if (haveDefaultPage)
            {
                route[pageParameterName] = defaultPageObject;
            }
            return MvcHtmlString.Create(result);
        }

        /// <summary>
        /// 创建指定页码
        /// </summary>
        /// <param name="url"></param>
        /// <param name="page"></param>
        /// <param name="pageParameterName"></param>
        /// <returns></returns>
        public static MvcHtmlString CreatePageUrl(this UrlHelper url, int page, string pageParameterName = "page")
        {
            return url.CreatePageUrl(url.RequestContext.RouteData.Values, page, pageParameterName);
        }

        public static RouteValueDictionary CreateCurrentRoute(this UrlHelper url)
        {
            RouteValueDictionary route = new RouteValueDictionary();
            foreach (string key in url.RequestContext.HttpContext.Request.QueryString.AllKeys)
            {
                route[key] = url.RequestContext.HttpContext.Request.QueryString.Get(key);
            }
            foreach (var item in url.RequestContext.RouteData.Values)
            {
                route[item.Key] = url.RequestContext.RouteData.Values[item.Key];
            }
            return route;
        }

        public static string GetQueryString(this UrlHelper url, string parameterName)
        {
            object parameterObject;
            bool isInRoute = url.RequestContext.RouteData.Values.TryGetValue(parameterName, out parameterObject);
            if (!isInRoute)
            {
                parameterObject = url.RequestContext.HttpContext.Request.QueryString[parameterName];
            }
            return Convert.ToString(parameterObject);
        }

        public static string ToFormatSubString(this string s, int length) 
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            if (s.Length <= length)
            {
                return s;
            }
            else
            {
                return s.Substring(0, length) + "...";
            }
        }
    }
}
